package cn.fz.relaxation.job.bilibili;

import cn.fz.relaxation.dao.entity.RealxHotSearchDO;
import cn.fz.relaxation.job.BaseHotSearchJob;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static cn.fz.relaxation.common.enums.HotSearchEnum.BILIBILI;

@Slf4j
@Component
public class BilibiliHotSearchJob extends BaseHotSearchJob {
    @Override
    @XxlJob("bilibiliHotSearchJob")
    public ReturnT<String> hotSearch(String param) throws IOException {
        log.info("B站热搜爬虫任务开始");
        this.resourceCode = BILIBILI.getCode();
        try {
            // 查询B站热搜数据
            OkHttpClient client = new OkHttpClient().newBuilder().build();
            Request request = new Request.Builder().url("https://api.bilibili.com/x/web-interface/ranking/v2")
                    .addHeader("User-Agent", "Mozilla/5.0 (compatible)").addHeader("Cookie", "b_nut=1712137652; "
                            + "buvid3=DBA9C433-8738-DD67-DCF5" + "-DDC780CA892052512infoc").method("GET", null).build();
            Response response = client.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            JSONArray array = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0, len = array.size(); i < len; i++) {
                // 获取B站热搜信息
                JSONObject object = (JSONObject)array.get(i);
                // 构建热搜信息榜
                RealxHotSearchDO searchDO = RealxHotSearchDO.builder().hotSearchResource(BILIBILI.getCode()).build();
                // 设置B站三方ID
                searchDO.setHotSearchId(object.getString("aid"));
                // 设置文章连接
                searchDO.setHotSearchUrl(object.getString("short_link_v2"));
                // 设置文章标题
                searchDO.setHotSearchTitle(object.getString("title"));
                // 设置作者名称
                searchDO.setHotSearchAuthor(object.getJSONObject("owner").getString("name"));
                // 设置作者头像
                searchDO.setHotSearchAuthorAvatar(object.getJSONObject("owner").getString("face"));
                // 设置文章封面
                searchDO.setHotSearchCover(object.getString("pic"));
                // 设置热搜热度
                searchDO.setHotSearchHeat(object.getJSONObject("stat").getString("view"));
                // 按顺序排名
                searchDO.setHotSearchOrder(i + 1);
                // 添加到集合
                searchDOList.add(searchDO);
            }
            if (CollectionUtils.isEmpty(searchDOList)){
                log.error("没有获取到B站数据");
                return ReturnT.FAIL;
            }
            // 数据加到缓存和数据持久化
            this.saveSearchListToDBAndMap();
            log.info("B站热搜爬虫任务结束");
        } catch (IOException e) {
            log.error("获取B站数据异常", e);
            // throw new RuntimeException(e);
        }
        return ReturnT.SUCCESS;
    }
}
